package com.mo.ld.activity.interactive

import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.listener.OnTimeSelectListener
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.dylanc.longan.activity
import com.mo.ld.R
import com.mo.ld.bean.ActionBean
import com.mo.ld.modle.getMainAdapter2
import com.mo.lib.ui.activity.BaseRvActivity
import com.mo.lib.ui.dialog.*
import com.mo.lib.utils.*
import com.mo.lib.utils.data.isEmpty
import com.wildma.pictureselector.PictureSelectUtils
import com.xuexiang.xaop.annotation.Permission
import com.xuexiang.xaop.consts.PermissionConsts
import com.xuexiang.xutil.data.DateUtils
import java.util.*

/**
 * @ author：mo
 * @ data：2021/9/23:10:23
 * @ 功能：弹窗
 */
class DialogsActivity : BaseRvActivity<ActionBean>() {
    private var viewById: ImageView? = null
    private val activityList: MutableList<ActionBean>
        private get() {
            return mutableListOf(
                ActionBean("BaseIosDialog") {
                    BaseIosDialog().setTitle("iOS风格").setMsg("内容")
                        .setLeftTextView("默认空串隐藏") { showToast("点击后自动关闭1") }
                        .setRightTextView("主意图串"){
                            showToast("点击后自动关闭2")
                    }
                        .setOnCancelListener {
                            KLog.i("setOnCancelListener")
                        }
                        .setOnShowListener {
                            KLog.i("setOnShowListener")
                        }
                        .setOnDismissListener {
                        KLog.i("setOnDismissListener")
                    }


                        .show()
                },
                ActionBean("BaseIosDialog2") {
                    BaseIosDialog().setTitle("iOS风格").setMsg("内容靠左"){
                        it.gravity=Gravity.LEFT
                    }.setMsg2("内容默认")
                        .setLeftTextView("默认空串隐藏") { showToast("点击后自动关闭1") }
                        .setRightTextView("主意图串") { showToast("点击后自动关闭2") }
                        .show()
                },
                ActionBean("BaseIosDialog左键空") {
                    BaseIosDialog().setTitle("iOS风格").setMsg("内容靠左"){
                        it.gravity=Gravity.LEFT
                    }.setMsg2("内容默认")
//                        .setLeftTextView("默认空串隐藏") { showToast("点击后自动关闭1") }
                        .setRightTextView("主意图串") { showToast("点击后自动关闭2") }
                        .show()
                },
                ActionBean("BaseIosDialog左键空2") {
                    BaseIosDialog().setMsg("当前余额" + "" + "元")
                        .setMsg2("充电可用余额不足，请您及时充值")
                        .setLeftTextView("知道了")
                        .setRightTextView("去充值") { showToast("dianji")}
                        .setOnDismissListener {
                            showToast("setOnDismissListener")
                        }
                        .show()
                },
                ActionBean("LoadingDialog") {
                    LoadingDialog("DDDDDDDD").show()
                },
                ActionBean("PhoneTakeDialog") {
                    object : PhoneTakeDialog(true) {

                        @Permission(PermissionConsts.STORAGE) override fun actionPhoneChoose() {
                            showToast("相册选")
                        }

                        @Permission(PermissionConsts.CAMERA) override fun actionPhoneTake() {
                            showToast("拍照")
                        }
                    }.show()
                },
                ActionBean("CarmeraDialog") {
                    CarmeraDialog() { isSave, file, path, bitmap -> Glide.with(activity).load(path).into((viewById)!!) }.show()
                },
                ActionBean("PickerView：日期选择") {
                    val startDate = Calendar.getInstance()
                    startDate[2018, 0] = 1
                    val endDate = Calendar.getInstance()
                    TimePickerBuilder(activity, object : OnTimeSelectListener {
                        override fun onTimeSelect(date: Date, view: View) {
                            showToast(DateUtils.date2String(date, DateUtils.yyyyMMddHHmmss.get()))
                        }
                    }).isCyclic(false).setCancelText("取消").setSubmitText("确定") //                        .setTitleText("选择月份")
                        .setRangDate(startDate, endDate).setDate(endDate).setType(booleanArrayOf(true, true, true, true, true, true)) // 默认全部显示
                        .build().show()
                },
                ActionBean("BaseDialog") {
                    object : BaseDialog(R.layout.item_main) {
                        override fun doWhat() {
                            findViewById<TextView>(R.id.tv_item_main).setClick {
                                dismisss()
                            }
                        }
                    }.setDismissType(DismissType.OUT).show()
                },
                ActionBean("权限提示弹窗") {
                    object : BaseIosDialog(){}.setMsg("向您申请定位权限！") .setLeftTextView("取消").setRightTextView("授权"){
                        showToast("去授权")
                    }.show()
                },
                ActionBean("权限提示弹窗") {
                    TipsPermissionDialog("拍照"){}.show()
//                    object : BaseIosDialog(){}.setMsg("向您申请定位权限！") .setLeftTextView("取消").setRightTextView("授权"){
//                        showToast("去授权")
//                    }.show()
                },
                )
        }

    override fun creatAdaper(): BaseQuickAdapter<ActionBean, BaseViewHolder> {
        return getMainAdapter2(activityList)

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            PictureSelectUtils.GET_BY_CAMERA -> {
                val picturePath = PictureSelectUtils.onActivityResult(activity, requestCode, resultCode, data, false, 0, 0, 0, 0)
                if (!isEmpty(picturePath)) {
                    KLog.i(picturePath)
                    val degree = getRotateDegree(picturePath)
                    if (degree != 0) {
                        saveFile(picturePath, getBitmapForRotate(BitmapFactory.decodeFile(picturePath), degree))
                    }
                    val bitmap = BitmapFactory.decodeFile(picturePath)
                    viewById!!.setImageBitmap(bitmap)
                }

            }
            PictureSelectUtils.GET_BY_ALBUM -> {
                val picturePath2 = PictureSelectUtils.onActivityResult(activity, requestCode, resultCode, data, false, 200, 200, 1, 1)
                KLog.i(picturePath2)
                Glide.with(activity).load(picturePath2).into(viewById!!)
            }
            else -> {
            }
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        val view = getView(R.layout.item_img)
        viewById = view.findViewById(R.id.iv_item_img)
        viewBinding?.llBaseListBottom?.addView(view)
    }

    override fun getList(page: Int) {}
    override fun getMore(page: Int) {}


}